package com.giser.testapp.lib.Tool;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.format.DateFormat;

import java.io.File;
import java.util.Calendar;
import java.util.Locale;

/**
 * Created by lss on 2017/11/28.
 */

public class FileHelper {
    /**
     * 内存卡路径
     */
    public static File getStorageDirectory() {
        return android.os.Environment.getExternalStorageDirectory();
    }
    //应用程序路径名称
    private static String appName="SuZhou";
    /**
     * 应用程序目录
     */
    public static File getApplicationDir() {
        File file = new File(getStorageDirectory().getAbsolutePath() + "/"+appName);
        if (!file.exists()) {
            file.mkdirs();
        }
        return file;
    }

    private static String pictureName="photo";
    /**
     * 图片存放目录
     */
    public static File getPhotoDir() {
        File file = new File(getApplicationDir().getAbsolutePath() + "/"+pictureName);
        if (!file.exists()) {
            file.mkdirs();
        }
        return file;
    }
    private static String MpkName="mpk";
    /**
     * 离线地图文件目录
     */
    public static File getMpkDir() {
        File file = new File(getApplicationDir().getAbsolutePath() + "/"+MpkName);
        if (!file.exists()) {
            file.mkdirs();
        }
        return file;
    }

    private static String ApkName="apk";
    /**
     * 安装包文件目录
     */
    public static File getApkDir() {
        File file = new File(getApplicationDir().getAbsolutePath() + "/"+ApkName);
        if (!file.exists()) {
            file.mkdirs();
        }
        return file;
    }

    /**
     * 获取随机图片文件名
     */
    public static String getRandomImagePath() {
        String pictureName = DateFormat.format("yyyyMMdd_hhmmss", Calendar.getInstance(Locale.CHINA)) + ".jpg";
        return getPhotoDir() + "/" + pictureName;
    }

    /**
     * 获取随机文件名（不包含扩展名）
     */
    public static String getRandomFileName() {
        return DateFormat.format("yyyyMMdd_hhmmss", Calendar.getInstance(Locale.CHINA)).toString();
    }

    /**获得扩展名
     * @param fileName xxxx.xx
     * @return xx
     * */
    public static String getFileExtension(String fileName)
    {
        String extension = "";
        if(fileName != null)
        {
            int splitIndex = fileName.lastIndexOf(".");
            if(splitIndex > 0)
            {
                extension = fileName.substring(splitIndex + 1);
            }
        }

        return extension;
    }

    /**
     * 安装包操作
     */
    public static boolean installApk(Context context, String filePath) {
        Intent i = new Intent(Intent.ACTION_VIEW);
        File file = new File(filePath);
        if (file != null && file.length() > 0 && file.exists() && file.isFile()) {
            i.setDataAndType(Uri.parse("file://" + filePath),
                    "application/vnd.android.package-archive");
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
            return true;
        }
        return false;
    }
}
